import Actor from './Actor.js';

/** 玩家类 */
export default class Player extends Actor {
    constructor(ctx, x, y, width, height, radius = 25, color = 'blue') {
        super(ctx, x, y, width, height, 'fillCircle', radius, color);
        /** 分数 */
        this.score = 0;

        /** 生存时间 */
        this.liveTime = 0;

        /** 运动步长 */
        this.resultStep = 15;
        this.stepX = this.resultStep;
        this.stepY = this.resultStep;
    }

    /** 左右移动 true表示向右移动 */
    moveX(direction) {
        this.stepX = direction ? this.resultStep : -this.resultStep;
    }

    /** 上下移动 true表示向下移动 */
    moveY(direction) {
        this.stepY = direction ? this.resultStep : -this.resultStep;
    }

    stopMoveX() {
        this.stepX = 0;
    }

    stopMoveY() {
        this.stepY = 0;
    }

    stopMove() {
        this.stepX = 0;
        this.stepY = 0;
    }

    update() {
        let { left, right } = this.isTouchingMapWallX(this.x + this.stepX);
        if (left || right) return;
        this.x += this.stepX;

        let { top, bottom } = this.isTouchingMapWallY(this.y + this.stepY);
        if (top || bottom) return;
        this.y += this.stepY;
    }

    draw() {
        this.liveTime++;
        super.draw();
    }
}
